<template>
  <el-dialog
    title="生成授权"
    :visible.sync="open"
    :before-close="cancel"
    width="800px"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" class="dialog-form">
      <el-form-item
        label="使用以下API链接来生成产品授权。该链接包含您的授权令牌，有效期1年，请妥善保管。"
        prop="key"
      >
        <el-input v-model="form.key" type="textarea" disabled placeholder="key" />
      </el-form-item>
      <div class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-document-copy"
          size="mini"
          @click="handleCopy"
          >复制</el-button
        >
      </div>
      <div class="tips">
        <p>使用说明:</p>
        <p>1.复制上面的API链接</p>
        <p>2.将链接中的seed参数替换为设备的MAC地址或其他唯一标识</p>
        <p>3.使用HTTP GET请求该链接</p>
        <p>4.获取返回的license信息，包含序列号等数据</p>
      </div>
      <div class="tips">
        <p>教程:</p>
        <p>
          <el-link type="primary" href="" target="_blank"
            >ESP-IDF 写入 License 授权</el-link
          >
        </p>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import { generateLicenseToken } from "@/api/login";
export default {
  props: {
    open: {
      type: Boolean,
      default: false,
    },
    id: {
      type: [Number, String],
      default: "",
    },
  },
  data() {
    return {
      form: {
        key: "",
      },
      rules: {},
    };
  },
  watch: {
    open: {
      handler(newVal, old) {
        if (newVal) {
          this.getGenerateLicenseToken();
        }
      },
    },
  },
  methods: {
    async getGenerateLicenseToken() {
      try {
        const { id } = this;
        const { message } = await generateLicenseToken(id);
        this.form.key = "https://ai.futeenergy.com" + message;
      } catch (error) {
        console.error("获取授权码错误", error);
      }
    },
    cancel() {
      this.$emit("cancel");
    },
    handleCopy() {
      const { key } = this.form;
      let input = document.createElement("input");
      input.value = key;
      document.body.appendChild(input);
      input.select();
      document.execCommand("copy");
      document.body.removeChild(input);
      this.$modal.msgSuccess("复制成功");
    },
  },
};
</script>

<style scoped lang="scss">
.firmware {
  margin: 0 0 22px 0;
  .firmware-top {
    margin-bottom: 22px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}
.upload-btn {
  display: block;
}
.el-form-item-upload {
  display: block;
  width: 100%;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
.tips {
  margin-top: 24px;
  background-color: #f5f5f5;
  padding: 16px;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1.5714285714285714;
}
.tips p {
  margin-bottom: 8px;
}
</style>

<style>
.el-form-item-upload-div .el-form-item__label {
  float: none !important;
}
</style>
